const util = require('../../../utils/util.js');
var user = require('../../../services/user.js');
const Toast = require('../../../utils/toast.js');
var app = getApp();
Page({
  data: {
    isLogin: false,
    headerSrc: '',
    userInfo: '',
    agreementShow: false,
    isRead: 0,
    username: '',
    certificate: '',
    certificate_number: '',
    contact: '',
    addInfo: "",
    sex: [{
        name: '男',
        value: 1,
        checked: 'true'
      },
      {
        name: '女',
        value: 2
      }
    ]
  },
  onLoad: function() {
    let token = wx.getStorageSync('token')
    console.log(token)
    if (token) {
      this.setData({
        isLogin: true
      })
    } else {
      wx.navigateTo({
        url: 'pages/login/login',
      })
    }
  },

  formSubmit: function(e) {
    let username = e.detail.value.username;
    let sex = e.detail.value.sex;
    let certificate = e.detail.value.certificate;
    let certificate_number = e.detail.value.certificate_number;
    let good_courses = e.detail.value.good_courses;
    let contact = e.detail.value.contact;
    let mobile = /^[1][3,4,5,7,8][0-9]{9}$/;
    // var myreg = /^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/;  //判断是否是座机电话
    if (!username) {
      Toast('请输入姓名', 'noicon');
      return;
    }
    if (!certificate) {
      Toast('请输入证件名称', 'noicon');
      return;
    }
    if (!certificate_number) {
      Toast('请输入证件号码', 'noicon');
      return;
    }
    if (!good_courses) {
      Toast('请输入我擅长的课程', 'noicon');
      return;
    }
    if (!contact) {
      Toast('请输入联系方式', 'noicon');
      return;
    }
    if (!mobile.exec(contact)) {
      Toast('请输入正确的联系方式', 'noicon');
      return;
    }
    console.log(e.detail.value)
    this.setData({
      agreementShow: true,
      username: username,
      certificate: certificate,
      certificate_number: certificate_number,
      contact: contact,
      addInfo: e.detail.value
    })
  },
  know() {
    let that = this;
    if (that.data.isRead === 1) {
      that.setData({
        agreementShow: false
      })
      Toast('请不要重复', 'noicon');
      return;
    }
    util.request('user/tobeteacher', {
      addinfo: that.data.addInfo
    }, 'POST').then(function(res) {
      console.log(res);
      if (res.errno === 0) {
        that.setData({
          agreementShow: false,
          isRead: 1
        })
        Toast(res.msg, 'success');
      }
    });
  }
})